// miniprogram/pages/main/address_select/index.js
const app = getApp()
var QQMapWX = require('./../../../sdk/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "地址获取中..."
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  selectMyAddress() {
    var params = {};
    params.no = 1;
    params.size = 50;
    app.dataRequest("/rest/customeraddress/my.htm", params, this, "addressList");
  },
  selectAddress(e) {
    console.info(e);
    var obj = e.currentTarget.dataset.object;
    app.address = obj.address;
    app.lat = obj.lat;
    app.lng = obj.lng;
    app.location=obj;
    app.setStorage("location", obj);
    wx.navigateBack({
      delta: 0,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  init() {
    var self = this;
    console.info("获取地址");
    this.reload = false;
    wx.getStorage({
      key: "location",
      success(result) {
        console.log(result);
        var res = result.data;
        if (res.address) {
          self.setData({
            address: res.address
          });
          app.address = res.address;
          app.lat = res.lat;
          app.lng = res.lng;
        } else {
          self.initLocationAddress();
        }

      },
      fail() {
        self.initLocationAddress();
      }
    })





  },
  initLocationAddress() {
    var self = this;
    qqmapsdk = new QQMapWX({
      key: 'LUCBZ-NE5KK-7UEJH-AFXWU-3Q6SO-PPFQI'
    });
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        console.log(res);
        qqmapsdk.reverseGeocoder({
          location: res,
          success: function (data) {
            console.log(data);
            if (data.result.address) {
              self.setData({
                address: data.result.address
              });
              var loc = {};
              loc.address = data.result.address;
              loc.lat = res.latitude;
              loc.lng = res.longitude;

              app.address = loc.address;
              app.lat = loc.lat;
              app.lng = loc.lng;
              app.location=loc;
              app.setStorage("location", loc);
              if (self.reload) {
                wx.navigateBack({
                  delta: 0,
                  success: (res) => {},
                  fail: (res) => {},
                  complete: (res) => {},
                })
              }

            }
          },
          fail: function (error) {
            console.error(error);
          },
          complete: function (res) {
            //console.log(res);
          }
        });

        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        app.lat = latitude;
        app.lng = longitude;
      }
    })

  },
  reloadAddress() {
    this.reload = true;
    this.initLocationAddress();
  },
  goSearch() {
    var self = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        if (res.name) {
          self.setData({
            address: res.name
          });

          var loc = {};
          loc.address = res.address;
          loc.lat = res.latitude;
          loc.lng = res.longitude;

          app.address = loc.address;
          app.lat = loc.lat;
          app.lng = loc.lng;
          app.setStorage("location", loc);
          wx.navigateBack({
            delta: 0,
            success: (res) => {},
            fail: (res) => {},
            complete: (res) => {},
          })
        }
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  nearby(res) {
    qqmapsdk.search({
      keyword: " ",
      location: res, //设置周边搜索中心点
      success: function (res) {
        console.info(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
    this.selectMyAddress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})